package self.niu.com.resume.utils;

import android.content.Context;
import android.util.Log;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

/**
 *
 * @说 明：
 * 
 *
 */
public class LocationHelper {
    private static LocationHelper helper;
    private final String TAG = "LocationHelper";
    private LocationClient locationClient;

    private BDLocationListener locationListener;

    private static Context context;

    private BdLocationCallback bdLocationCallback;


    public static void init(Context context) {
        LocationHelper.context = context;
    }
    /**
     *
     */
    private LocationHelper() {
        if (context == null) {
            Log.e(TAG, "请先调用一下init(context)方法");
            return;
        }
        locationClient = new LocationClient(context, getLocOption());
        locationListener = new AppBdLocationListener();
        locationClient.registerLocationListener(locationListener);

    }


    private LocationClientOption getLocOption() {
        LocationClientOption locationClientOption = new LocationClientOption();
        //指定定位模式
        locationClientOption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        //设置坐标的系的类型(必须)
        locationClientOption.setCoorType("bd09ll");
        locationClientOption.setScanSpan(5000);
        locationClientOption.setIsNeedAddress(true);
        locationClientOption.setOpenGps(true);
        return locationClientOption;
    }

    public static LocationHelper getInstance() {
        if (helper == null) {
            helper = new LocationHelper();
        }
        return helper;
    }

    private class AppBdLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            //回调方法执行频率根据scanspan 设置的参数有关
            double lat = bdLocation.getLatitude();
            double lngt = bdLocation.getLongitude();
            String address = bdLocation.getCity();
            String a = bdLocation.getCountry();
            String b = bdLocation.getStreet();
            if (bdLocationCallback != null) {
                bdLocationCallback.locationCallback(lat, lngt,address+a+b);
            }
        }
    }
    /**
     *
     */
    public void start() {
        locationClient.start();

    }

    public void stop() {
        if (locationClient != null) {
            locationClient.stop();
            if (locationListener != null) {
                locationClient.unRegisterLocationListener(locationListener);
            }
            helper = null;
        }
    }

    public interface BdLocationCallback {
         void locationCallback(double lat, double lngt, String address);
    }
    public void setBdLocationCallback(BdLocationCallback bdLocationCallback) {
        this.bdLocationCallback = bdLocationCallback;
    }
}
